//
//  MainTabBarController.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/2.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import ESTabBarController_swift
import PKHUD

class MainTabBarController: ESTabBarController {
  
  // MARK: - IBOutlets
  
  
  
  // MARK: - Private vars
  
  var playerItem: ESTabBarItem?
  
  // MARK: - Private methods
  
  private func prepare() {
    // noremal items
    let ghostWorldNavigationController = UIStoryboard(name: "GhostWorld", bundle: nil).instantiateViewController(withIdentifier: "GhostWorldNavigationController")
    let mineNavigationController = UIStoryboard(name: "Mine", bundle: nil).instantiateViewController(withIdentifier: "MineNavigationController")
    let memeberNavigationController = UIStoryboard(name: "VIP", bundle: nil).instantiateViewController(withIdentifier: "VIPNavigationController")
    let accountNavigationController = UIStoryboard(name: "Account", bundle: nil).instantiateViewController(withIdentifier: "AccountNavigationController")
    
    func tabBarItem(_ contentView: ESTabBarItemContentView, title: String?, imageName: String, selectedImageName: String, tag: Int) -> ESTabBarItem {
      return ESTabBarItem(contentView, title: title, image: UIImage(named: imageName), selectedImage: UIImage(named: selectedImageName), tag: tag)
    }
    ghostWorldNavigationController.tabBarItem = tabBarItem(BasicContentView(), title: "鬼影人间", imageName: "TabBarGW", selectedImageName: "TabBarGW_HL", tag: 0)
    mineNavigationController.tabBarItem = tabBarItem(BasicContentView(), title: "我的", imageName: "TabBarMine", selectedImageName: "TabBarMine_HL", tag: 1)
    memeberNavigationController.tabBarItem = tabBarItem(MemberContentView(), title: "会员", imageName: "TabBarMember", selectedImageName: "TabBarMember_HL", tag: 3)
    accountNavigationController.tabBarItem = tabBarItem(BasicContentView(), title: "账号", imageName: "TabBarAccount", selectedImageName: "TabBarAccount_HL", tag: 4)
    
    // player item
    let playerPlaceholder = UIViewController()
    playerItem = ESTabBarItem(PlayerContentView(), title: nil, image: UIImage(named: "TabBarPlayerPlay"), selectedImage: UIImage(named: "TabBarPlayerPlay"), tag: 2)
    playerPlaceholder.tabBarItem = playerItem
    
    // tabbar
    tabBar.backgroundImage = UIImage(named: "TabBarBg")
    shouldHijackHandler = { tabbarController, viewController, index in
      if index == 2 {
        return true
      }
      return false
    }
    didHijackHandler = { [weak self] tabbarController, viewController, index in
      if index == 2 {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
          if let _ = NowPlayingManager.shared.audio,
            let _ = NowPlayingManager.shared.album {
            let playerNavigationController = UIStoryboard(name: "Player", bundle: nil).instantiateViewController(withIdentifier: "PlayerNavigationController") as! BaseNavigationController
            playerNavigationController.dismissDelegate = self
            tabbarController.present(playerNavigationController, animated: true, completion: nil)
          } else {
            HUD.flash(.label("没有播放声音"), delay: 1.5)
          }
        }
      }
    }
    viewControllers = [
      ghostWorldNavigationController,
      memeberNavigationController,
      playerPlaceholder,
      mineNavigationController,
      accountNavigationController
    ]
    
    // tabbar shadow
    tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
    tabBar.layer.shadowRadius = 10
    tabBar.layer.shadowColor = UIColor.black.cgColor
    tabBar.layer.shadowOpacity = 0.5
    
  }
  
  @objc private func popAllToRoot() {
    viewControllers?.forEach {
      if type(of: $0) == UINavigationController.self {
        ($0 as! UINavigationController).popToRootViewController(animated: false)
      }
      if type(of: $0) == BaseNavigationController.self {
        ($0 as! BaseNavigationController).popToRootViewController(animated: false)
      }
    }
  }
  
  // MARK: - Public vars
  
  
  
  // MARK: - Lifecycle
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self, selector: #selector(popAllToRoot), name: .didLogout, object: nil)
    prepare()
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  deinit {
    NotificationCenter.default.removeObserver(self)
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
}

extension MainTabBarController: BaseNavigationControllerDismissDelegate {
  func dismiss(navigationController: BaseNavigationController, completion: (() -> Void)?) {
    dismiss(animated: true, completion: completion)
  }
}
